<html>
<head><meta charset="utf-8"><title>array default impls · project-const-generics · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/index.html">project-const-generics</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html">array default impls</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236871360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236871360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236871360">(Apr 30 2021 at 17:25)</a>:</h4>
<p>we've talked a bit about generic <code>Default</code> impls for arrays in the last meeting and it was also mentioned in #t-libs recently. And I have now come up with an imo clean solution here</p>



<a name="236871432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236871432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236871432">(Apr 30 2021 at 17:25)</a>:</h4>
<p>we have 2 requirements for these impls</p>



<a name="236871476"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236871476" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236871476">(Apr 30 2021 at 17:25)</a>:</h4>
<ul>
<li>don't break <code>[Foo; 0]</code> where <code>Foo</code> doesn't impl <code>Default</code></li>
</ul>



<a name="236871557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236871557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236871557">(Apr 30 2021 at 17:26)</a>:</h4>
<ul>
<li>allow for <code>fn foo&lt;T: Default, const N: usize&gt;() -&gt; [T; N] { Default::default() }</code></li>
</ul>



<a name="236871941"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236871941" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236871941">(Apr 30 2021 at 17:29)</a>:</h4>
<p>in <a href="https://github.com/rust-lang/rust/issues/74254">#74254</a> we implemented this using</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[marker]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">ArrayDefault</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">ArrayDefault</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Default</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="n">ArrayDefault</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">DefaultHack</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">default_hack</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">DefaultHack</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">default</span> <span class="k">fn</span><span class="w"> </span><span class="n">default_hack</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="fm">unreachable!</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Default</span><span class="o">&gt;</span><span class="w"> </span><span class="n">DefaultHack</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">default_hack</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Default</span>::<span class="n">default</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Default</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"></span>
<span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span>: <span class="nc">ArrayDefault</span><span class="p">,</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">default</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="p">[();</span><span class="w"> </span><span class="n">N</span><span class="p">].</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="n">_unit</span><span class="o">|</span><span class="w"> </span><span class="n">DefaultHack</span>::<span class="n">default_hack</span><span class="p">())</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="236872021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236872021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236872021">(Apr 30 2021 at 17:30)</a>:</h4>
<p>this depends on both <code>feature(marker_trait_attr)</code> and <code>feature(specialization)</code></p>



<a name="236872093"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236872093" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236872093">(Apr 30 2021 at 17:30)</a>:</h4>
<p>the 2 issues with this approach were:</p>



<a name="236872318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236872318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236872318">(Apr 30 2021 at 17:31)</a>:</h4>
<ul>
<li>specializing on <code>Default</code>, while fine here, is generally unsound, so using this in <code>core</code> is definitely kinda sus</li>
</ul>



<a name="236872474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236872474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236872474">(Apr 30 2021 at 17:32)</a>:</h4>
<ul>
<li>this generic implementation has slightly worse codegen than <code>[Default::default(), Default::default(), Default::default(), ...]</code>, which while imo acceptable is not great</li>
</ul>



<a name="236872681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236872681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236872681">(Apr 30 2021 at 17:33)</a>:</h4>
<p>instead of using <code>DefaultHack</code>, we can however implement default for arrays as a compiler intrinsic and always using <code>Default::default()</code>there. This removes the reliance on specialization and fixes the codegen regression</p>



<a name="236873045"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236873045" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236873045">(Apr 30 2021 at 17:36)</a>:</h4>
<p>for the sake of <code>T-lang</code> we probably want to stabilize <code>feature(marker_trait_attr)</code> first, cc <span class="user-mention" data-user-id="116009">@nikomatsakis</span> do you know whether <a href="https://github.com/rust-lang/rust/issues/29864#issuecomment-660323818">https://github.com/rust-lang/rust/issues/29864#issuecomment-660323818</a> is still up to date?</p>



<a name="236873203"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236873203" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236873203">(Apr 30 2021 at 17:37)</a>:</h4>
<p>the one open issue here mentioned by <span class="user-mention" data-user-id="224872">@rylev</span> was about negative trait impls, or in general, whether we can loosen the orphan rules</p>



<a name="236873504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236873504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236873504">(Apr 30 2021 at 17:39)</a>:</h4>
<p>looking at <a href="https://github.com/rust-lang/rust/issues/67919#issuecomment-575315025">https://github.com/rust-lang/rust/issues/67919#issuecomment-575315025</a> it does not seem sound to greatly loosen orphan rules for marker traits, thereby solving this issue</p>



<a name="236873687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236873687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236873687">(Apr 30 2021 at 17:40)</a>:</h4>
<p>I remember there were some concerns about negative trait impls and a few other things</p>



<a name="236874568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236874568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236874568">(Apr 30 2021 at 17:46)</a>:</h4>
<p>hmm, as an alternative to stabilizing <code>marker_trait_attr</code></p>



<a name="236874575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236874575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236874575">(Apr 30 2021 at 17:46)</a>:</h4>
<p>we can also use <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=120539b4fb296036ca04b4141d903c34">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=120539b4fb296036ca04b4141d903c34</a></p>



<a name="236874621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236874621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236874621">(Apr 30 2021 at 17:47)</a>:</h4>
<p>this fulfills all requirements</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">marker</span>::<span class="n">PhantomData</span><span class="p">;</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">ArrayDefault</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">DefaultToSend</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="p">(),</span><span class="w"> </span><span class="n">PhantomData</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">);</span><span class="w"></span>
<span class="k">unsafe</span><span class="w"> </span><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Default</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Send</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">DefaultToSend</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">SendToDefault</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">(</span><span class="n">DefaultToSend</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">);</span><span class="w"></span>
<span class="k">unsafe</span><span class="w"> </span><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Send</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">SendToDefault</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="n">ArrayDefault</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="n">SendToDefault</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">N</span><span class="o">&gt;</span>: <span class="nb">Send</span> <span class="p">{}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">check_array_default</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">ArrayDefault</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">generic</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Default</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">check_array_default</span>::<span class="o">&lt;</span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="o">&gt;</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">zero</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">check_array_default</span>::<span class="o">&lt;</span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="mi">0</span><span class="p">]</span><span class="o">&gt;</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">non_zero_err</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">check_array_default</span>::<span class="o">&lt;</span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="mi">1</span><span class="p">]</span><span class="o">&gt;</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="236874736"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236874736" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236874736">(Apr 30 2021 at 17:47)</a>:</h4>
<p>oh my</p>



<a name="236880591"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236880591" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236880591">(Apr 30 2021 at 18:27)</a>:</h4>
<p>I love it</p>



<a name="236887969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236887969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236887969">(Apr 30 2021 at 19:21)</a>:</h4>
<p>ouch... this took me a bit to understand. I am in awe</p>



<a name="236889624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236889624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236889624">(Apr 30 2021 at 19:34)</a>:</h4>
<p>hold up, how do we implement the default function in the impl block? We can't access Default, as that bound doesn't exist at that level</p>



<a name="236889827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236889827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236889827">(Apr 30 2021 at 19:36)</a>:</h4>
<p>ah, we still need specialization for that?</p>



<a name="236893388"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236893388" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236893388">(Apr 30 2021 at 20:02)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/260443-project-const-generics/topic/array.20default.20impls/near/236889827">said</a>:</p>
<blockquote>
<p>ah, we still need specialization for that?</p>
</blockquote>
<p>my idea is an intrinsic</p>



<a name="236893492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236893492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236893492">(Apr 30 2021 at 20:03)</a>:</h4>
<p>so we use <code>unchecked_array_default&lt;T, const N: usize() -&gt;  [T; N]</code> which unconditionally uses <code>T::default()</code> for <code>N &gt; 0</code></p>



<a name="236949516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/236949516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#236949516">(May 01 2021 at 08:24)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/260443-project-const-generics/topic/array.20default.20impls/near/236873687">said</a>:</p>
<blockquote>
<p>I remember there were some concerns about negative trait impls and a few other things</p>
</blockquote>
<p>to be clear, I also <em>think</em> these weren't especially hard to resolve</p>



<a name="237063641"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237063641" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237063641">(May 02 2021 at 16:17)</a>:</h4>
<blockquote>
<p>my idea is an intrinsic</p>
</blockquote>
<p>that's not as trivial as i had hoped <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="237064108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237064108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237064108">(May 02 2021 at 16:25)</a>:</h4>
<p>:thonk:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">array_default_hack</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">fn</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="p">;</span><span class="w"></span>
</code></pre></div>



<a name="237064109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237064109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237064109">(May 02 2021 at 16:26)</a>:</h4>
<p>that should be easier <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="237067359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237067359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237067359">(May 02 2021 at 17:20)</a>:</h4>
<p>yes, simpler intrinsics are very much preferrable over ones that have loops and stuff in their expansion</p>



<a name="237071797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237071797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237071797">(May 02 2021 at 18:31)</a>:</h4>
<p>using a lang item and specialcasing in <code>resolve</code>, similar to <code>drop_in_place</code>, was a lot easier</p>



<a name="237072301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237072301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237072301">(May 02 2021 at 18:39)</a>:</h4>
<p>oh no, slowly getting broken by missing stability attributes :(</p>



<a name="237072328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237072328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237072328">(May 02 2021 at 18:40)</a>:</h4>
<p>also recompiling all of rustc everytime i change anything in my <code>cfg(not(bootstrap))</code> impl is also not as much fun as i would like. should start using <code>keep-stage</code></p>



<a name="237083661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237083661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237083661">(May 02 2021 at 21:06)</a>:</h4>
<p>okay, there's one unfun issue here though</p>



<a name="237083691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237083691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237083691">(May 02 2021 at 21:06)</a>:</h4>
<p>the auto trait hack does work how we want it to in nearly all cases</p>



<a name="237083697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237083697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237083697">(May 02 2021 at 21:06)</a>:</h4>
<p>but</p>



<a name="237083718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237083718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237083718">(May 02 2021 at 21:06)</a>:</h4>
<p>we have 1 explicit impl</p>



<a name="237083787"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237083787" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237083787">(May 02 2021 at 21:07)</a>:</h4>
<p>and one impl if all fields are <code>Send</code></p>



<a name="237084052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237084052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237084052">(May 02 2021 at 21:08)</a>:</h4>
<p>these automatic impls are only considered if there are no other potential impls</p>



<a name="237084197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237084197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237084197">(May 02 2021 at 21:09)</a>:</h4>
<p>which means that if we make the impl for <code>0</code> the explicit one and the <code>T: Default</code> the auto one</p>



<a name="237084303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237084303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237084303">(May 02 2021 at 21:10)</a>:</h4>
<p>we "force" const inference variables to <code>0</code>, breaking code which should work</p>



<a name="237084395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237084395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237084395">(May 02 2021 at 21:11)</a>:</h4>
<p>i don't think we do any inference stuff based on trait bounds, so making the <code>T: Default</code> bound the explicit one probably doesn't have the same issue</p>



<a name="237085606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237085606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237085606">(May 02 2021 at 21:18)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/260443-project-const-generics/topic/array.20default.20impls/near/237083661">said</a>:</p>
<blockquote>
<p>okay, there's one unfun issue here though</p>
</blockquote>
<p><del>unfun</del> I have to admit i really enjoyed that <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="237090639"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237090639" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237090639">(May 02 2021 at 22:32)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/84838">#84838</a></p>



<a name="237093619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/237093619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#237093619">(May 02 2021 at 23:19)</a>:</h4>
<p>ok, we do need actual marker traits <span aria-label="sparkles" class="emoji emoji-2728" role="img" title="sparkles">:sparkles:</span></p>



<a name="238518830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/238518830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#238518830">(May 12 2021 at 18:08)</a>:</h4>
<p>I also give this a try, without using markers or intrinsics. But it seems i've got something wrong...</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(const_generics)]</span><span class="w"></span>
<span class="cp">#![allow(incomplete_features)]</span><span class="w"></span>

<span class="k">use</span><span class="w"> </span><span class="n">core</span>::<span class="n">marker</span>::<span class="n">PhantomData</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">DefaultArrayBuilder</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">LEN</span>: <span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">IS_EMPTY</span>: <span class="kt">bool</span><span class="o">&gt;</span><span class="p">(</span><span class="n">PhantomData</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">);</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">DefaultArrayStrategy</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">F</span>: <span class="nb">Default</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">G</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">make_array</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span>::<span class="n">G</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">LEN</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="n">DefaultArrayStrategy</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">DefaultArrayBuilder</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">LEN</span><span class="p">,</span><span class="w"> </span><span class="kc">true</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">F</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">LEN</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">make_array</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">LEN</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="fm">assert_eq!</span><span class="p">(</span><span class="n">LEN</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">zeroed</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">LEN</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="n">DefaultArrayStrategy</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">DefaultArrayBuilder</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">LEN</span><span class="p">,</span><span class="w"> </span><span class="kc">false</span><span class="o">&gt;</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="n">T</span>:<span class="nb">Default</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">F</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">T</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">LEN</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">make_array</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">LEN</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="p">[</span><span class="n">T</span>::<span class="n">default</span><span class="p">();</span><span class="w"> </span><span class="n">LEN</span><span class="p">]</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">MyDefault</span>: <span class="nb">Sized</span> <span class="p">{</span><span class="w"> </span><span class="k">fn</span> <span class="nf">default</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">LEN</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="n">MyDefault</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">LEN</span><span class="p">]</span><span class="w"> </span><span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="n">DefaultArrayBuilder</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">LEN</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="n">LEN</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">}</span><span class="o">&gt;</span>: <span class="nc">DefaultArrayStrategy</span><span class="o">&lt;</span><span class="n">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">Self</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="o">&lt;</span><span class="n">DefaultArrayBuilder</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">LEN</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="n">LEN</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">}</span><span class="o">&gt;</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">DefaultArrayStrategy</span><span class="o">&gt;</span>::<span class="n">F</span>: <span class="nb">Default</span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">default</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">&lt;</span><span class="n">DefaultArrayBuilder</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">LEN</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="n">LEN</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">}</span><span class="o">&gt;</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">DefaultArrayStrategy</span><span class="o">&lt;</span><span class="n">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">Self</span><span class="o">&gt;&gt;</span>::<span class="n">make_array</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="sd">/// error[E0391]: cycle detected when simplifying constant for the type system `&lt;impl at src/lib.rs:33:1: 40:2&gt;::{constant#1}`</span>
</code></pre></div>



<a name="238528032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/238528032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#238528032">(May 12 2021 at 19:07)</a>:</h4>
<p><span class="user-mention" data-user-id="116458">@Charles Lew</span> this is a really clever solution which should work but is broken because <code>{ LEN == 0 }</code> is part of its own where bounds</p>



<a name="238529652"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/238529652" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#238529652">(May 12 2021 at 19:19)</a>:</h4>
<p>i don't know why this cycle happens in this example specifically, but we noted the general problem in <a href="https://github.com/rust-lang/project-const-generics/blob/master/design-docs/anon-const-substs.md">https://github.com/rust-lang/project-const-generics/blob/master/design-docs/anon-const-substs.md</a></p>



<a name="242455101"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/242455101" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#242455101">(Jun 12 2021 at 15:01)</a>:</h4>
<p>I tried it again on recent nightly and it seems the error above is gone.<br>
I have a branch at <a href="https://github.com/crlf0710/rust/tree/array-default">https://github.com/crlf0710/rust/tree/array-default</a></p>
<p>Building core itself is ok, but there seems to be some trait resolution errors in downstream crates.</p>
<div class="codehilite" data-code-language="Text only"><pre><span></span><code>error[E0277]: the trait bound `array::array_default_impl::ArrayDefaultImplDispatch&lt;UnwindTableRow&lt;_&gt;, 4_usize, {_: bool}&gt;: array::array_default_impl::ArrayDefaultImpl` is not satisfied
    --&gt; L:\Zoo\Rust\cargo\registry\src\mirrors.sjtug.sjtu.edu.cn-4f7dbcce21e258a2\gimli-0.23.0\src\read\cfi.rs:1835:28
     |
1835 |             stack_storage: Default::default(),
     |                            ^^^^^^^^^^^^^^^^ the trait `array::array_default_impl::ArrayDefaultImpl` is not implemented for `array::array_default_impl::ArrayDefaultImplDispatch&lt;UnwindTableRow&lt;_&gt;, 4_usize, {_: bool}&gt;`
     |
     = help: the following implementations were found:
               &lt;array::array_default_impl::ArrayDefaultImplDispatch&lt;T, L, false&gt; as array::array_default_impl::ArrayDefaultImpl&gt;
               &lt;array::array_default_impl::ArrayDefaultImplDispatch&lt;T, L, true&gt; as array::array_default_impl::ArrayDefaultImpl&gt;
     = note: required because of the requirements on the impl of `Default` for `[UnwindTableRow&lt;_&gt;; 4]`
     = note: required by `rustc_std_workspace_core::default::Default::default`
</code></pre></div>



<a name="242455870"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/242455870" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#242455870">(Jun 12 2021 at 15:15)</a>:</h4>
<p>I guess trait stuff doesn't know that you can exhaustively define all generic impls?</p>



<a name="242456018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/242456018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#242456018">(Jun 12 2021 at 15:18)</a>:</h4>
<p>jup:</p>



<a name="242456037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/242456037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#242456037">(Jun 12 2021 at 15:19)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">Foo</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">T</span>: <span class="kt">bool</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Foo</span><span class="o">&lt;</span><span class="kc">true</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Foo</span><span class="o">&lt;</span><span class="kc">false</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">meh</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">C</span>: <span class="kt">bool</span><span class="o">&gt;</span><span class="p">(</span><span class="n">f</span>: <span class="nc">Foo</span><span class="o">&lt;</span><span class="n">C</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">muh</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">muh</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">Bar</span><span class="o">&gt;</span><span class="p">(</span><span class="n">_</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>



<a name="242456047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/242456047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#242456047">(Jun 12 2021 at 15:19)</a>:</h4>
<div class="codehilite"><pre><span></span><code>error[E0277]: the trait bound `Foo&lt;C&gt;: Bar` is not satisfied
  --&gt; src/lib.rs:9:9
   |
9  |     muh(f)
   |         ^ the trait `Bar` is not implemented for `Foo&lt;C&gt;`
...
12 | fn muh&lt;T: Bar&gt;(_: T) {}
   |           --- required by this bound in `muh`
   |
   = help: the following implementations were found:
             &lt;Foo&lt;false&gt; as Bar&gt;
             &lt;Foo&lt;true&gt; as Bar&gt;
</code></pre></div>



<a name="242461392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/242461392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#242461392">(Jun 12 2021 at 17:11)</a>:</h4>
<p>I feel my code doesn't really rely on the exhaustiveness?<br>
My current version is this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[cfg(not(bootstrap))]</span><span class="w"></span>
<span class="cp">#[stable(feature = </span><span class="s">"array_default"</span><span class="cp">, since = </span><span class="s">"1.55.0"</span><span class="cp">)]</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">LEN</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Default</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">LEN</span><span class="p">]</span><span class="w"></span>
<span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="n">ArrayDefaultImplDispatch</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">LEN</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">LEN</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">}</span><span class="o">&gt;</span>: <span class="nc">ArrayDefaultImpl</span><span class="o">&lt;</span><span class="n">Output</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">Self</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">default</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">&lt;</span><span class="n">ArrayDefaultImplDispatch</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">LEN</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">LEN</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">}</span><span class="o">&gt;</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">ArrayDefaultImpl</span><span class="o">&gt;</span>::<span class="n">default_array</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="242483030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/242483030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#242483030">(Jun 13 2021 at 01:36)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=0e5c6ed98c5f86601478d41643d3ec76">playground repro</a></p>



<a name="242492777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/242492777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#242492777">(Jun 13 2021 at 06:04)</a>:</h4>
<p>Converted this into an issue... <a href="https://github.com/rust-lang/rust/issues/86259">https://github.com/rust-lang/rust/issues/86259</a></p>



<a name="242509181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/242509181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#242509181">(Jun 13 2021 at 13:00)</a>:</h4>
<blockquote>
<p>I feel my code doesn't really rely on the exhaustiveness?</p>
</blockquote>



<a name="242509199"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/242509199" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#242509199">(Jun 13 2021 at 13:00)</a>:</h4>
<p>hmm, right</p>



<a name="242509212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/array%20default%20impls/near/242509212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/array.20default.20impls.html#242509212">(Jun 13 2021 at 13:00)</a>:</h4>
<p>looks like we are not evaluating it</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>